package com.sctvcloud.linshui.ui.activities;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Pair;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.google.gson.Gson;
import com.gridsum.tracker.GridsumWebDissector;
import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.ui.dialog.ShareFragment;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.ruihang.generalibrary.ui.widget.CustomEXImageView;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.ui.widget.CustomLinearLayout;
import com.ruihang.generalibrary.utils.GroupOrderHelperSingled;
import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.linshui.R;
import com.sctvcloud.linshui.application.Constances;
import com.sctvcloud.linshui.base.BaseActivity;
import com.sctvcloud.linshui.base.BaseDetailActivity;
import com.sctvcloud.linshui.beans.FOrganDetail;
import com.sctvcloud.linshui.beans.FScribeState;
import com.sctvcloud.linshui.beans.FUsers;
import com.sctvcloud.linshui.beans.FocusColumn;
import com.sctvcloud.linshui.beans.GovDetailBean;
import com.sctvcloud.linshui.beans.GovItemBean;
import com.sctvcloud.linshui.beans.HomeListItem;
import com.sctvcloud.linshui.beans.NewsClickCountBean;
import com.sctvcloud.linshui.beans.NewsIdBean;
import com.sctvcloud.linshui.beans.NewsItem;
import com.sctvcloud.linshui.beans.NewsListClickRequestBean;
import com.sctvcloud.linshui.beans.NewsListClickVo;
import com.sctvcloud.linshui.http.AbsListNetCallback;
import com.sctvcloud.linshui.http.AbsNetCallBack;
import com.sctvcloud.linshui.http.NetUtils;
import com.sctvcloud.linshui.http.ParamsEditor;
import com.sctvcloud.linshui.ui.adapter.CultureTravelAdapter;
import com.sctvcloud.linshui.ui.adapter.GovAdapter;
import com.sctvcloud.linshui.ui.adapter.holder.BaseAbsHolder;
import com.sctvcloud.linshui.ui.util.IListShowData;
import com.sctvcloud.linshui.ui.utils.GlideCircleTransform;
import com.sctvcloud.linshui.ui.utils.GlideUtil;
import com.sctvcloud.linshui.ui.utils.TitleUtils;
import com.sctvcloud.linshui.utils.ListTypeUtils;
import com.sctvcloud.linshui.utils.UserManager;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 文旅模板
 */
public class CultureTravelActivity extends BaseDetailActivity implements CanRefreshLayout.OnRefreshListener, CanRefreshLayout.OnLoadMoreListener {


    @BindView(R.id.title_top_title)
    protected CustomFontTextView title_top_title;

    @BindView(R.id.gov_refresh)
    protected CanRefreshLayout refreshLayout;
    @BindView(R.id.can_content_view)
    protected RecyclerView recyclerViewList;
    public static final String CHANNEL_ID ="channel_id";
    public static final String CHANNEL_NAME = "channel_name";
    private int mPageNo = 0;
    private int mPageAll = 0;
    private final static int pageSize = 10;
    private boolean mIsLoading = false;
    private GovAdapter mAdapter;
    private String title;
    private String channelId;

    private List<IListShowData> GovList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        title = getIntent().getStringExtra(CHANNEL_NAME);
        channelId = getIntent().getStringExtra(CHANNEL_ID);
        initView();
    }

    private void initView(){
        title_top_title.setVisibility(View.VISIBLE);
        title_top_title.setText(title);

        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);
        refreshLayout.setLoadMoreEnabled(false);
        recyclerViewList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyclerViewList.setItemAnimator(new DefaultItemAnimator());
//        rebllion_list.addItemDecoration(new RBaseItemDecoration(2));
        refreshLayout.autoRefresh();

    }

    @Override
    protected void initInject() {
        setContentView(R.layout.activity_culture_travel);
        butterUnbinder = ButterKnife.bind(this);
    }

    @OnClick({R.id.title_top_back })
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_top_back:
                finish();
                break;
        }
    }


    @Override
    public void onLoadMore() {
        mPageNo++;
        getData();
    }

    @Override
    public void onRefresh() {
        if (mIsLoading) {
            return;
        }
        mPageNo = 0;
        mIsLoading = true;
        GovList.clear();
        getData();
    }


    private void getData() {
        HashMap<String, Object> param = new HashMap<>();
        param.put("page", mPageNo);
        param.put("size", pageSize);
        param.put("siteNumber", Constances.SITE_ID);
        param.put("nodeId", channelId);

        NetUtils.getNetAdapter().getOrgInfos(getOwnerName(), param, new AbsNetCallBack<GovDetailBean>(GovDetailBean.class) {
            @Override
            public void onSuc(GovDetailBean fRebellions) {
                if (mPageAll == 0) {
                    mPageAll = fRebellions.getPageAll();
                }

                initData(fRebellions);
            }

            @Override
            public void onError(Throwable ex, String errorMsg) {
                toast(R.string.loading_no_data);
                if(mPageNo>0){
                    mPageNo--;
                }
            }

            @Override
            public void onEnd() {
                onNetFinish();
            }
        });
    }

    private synchronized void onNetFinish() {
        mIsLoading = false;
        refreshLayout.refreshComplete();
        refreshLayout.loadMoreComplete();
        refreshLayout.setLoadMoreEnabled(mPageNo+1 < mPageAll);
    }

    private void initData(GovDetailBean fRebellions) {

        if (ListUtils.isListValued(fRebellions.getRollImg())) {
            HomeListItem hb = new HomeListItem();
            hb.setNewsItemList(fRebellions.getRollImg());
            hb.setViewType(GovAdapter.VIEWTYPE_BANNER);
            GovList.add(hb);
        }

        if (ListUtils.isListValued(fRebellions.getDataList())) {
            GovList.addAll(fRebellions.getDataList());
        }

        if (mAdapter == null) {
            mAdapter = new GovAdapter(this, GovList);
            mAdapter.setItemInternalClick(itemClick);
            recyclerViewList.setAdapter(mAdapter);
            mAdapter.setActivity(this);
        }else{
            mAdapter.setData(GovList);
        }
    }


    OnItemInternalClick itemClick = new OnItemInternalClick() {
        @Override
        public void onItemInternalClick(View itemView, View view, int position) {
            switch (view.getId()) {
//                case R.id.tv_anchor_comment:
//                    ArrayList<Pair<String, String>> pairs = new ArrayList<>();
//                    pairs.add(new Pair<>("页面", "首页"));
//                    pairs.add(new Pair<>("栏目", "市州"));
//                    pairs.add(new Pair<>("位置", "专题名称"));
//                    ArrayList<Pair<String, String>> pairsMap = new ArrayList<>();
//                    pairsMap.addAll(pairs);
//                    pairsMap.add(new Pair<>("行为类型", "点击"));
//                    GridsumWebDissector.getInstance().trackEvent(activity, "", titleName, "", 200, pairsMap);
//                    HashMap<String, String> map = new HashMap<>();
//                    for (Pair<String, String> p : pairs) {
//                        map.put(p.first, p.second);
//                    }
//                    SkipUtil.skipWithNewsItem(context, newsItem, map);
//                    break;

            }
        }
    };



    @Override
    public void onBackPressed() {
        setResultes();
        super.onBackPressed();
    }

    private void setResultes() {
        Intent intent = getIntent();
        setResult(RESULT_OK, intent);
        finish();
    }
}
